package com.myCinema.biz.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myCinema.dao.impl.CategoryDaoImpl;
import com.myCinema.dao.impl.MovieDaoImpl;
import com.myCinema.entity.Category;
import com.myCinema.entity.Movie;

public class MovieListServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Category> categoryList = new CategoryDaoImpl().getAll();

		int pageNum = 1;
		int pageSize = 3;
		String title = "";
		int cid = 0 ;
		
		if(request.getParameter("title")!=null && !request.getParameter("title").isEmpty())
			title = request.getParameter("title");
		if(request.getParameter("pageNum")!=null && !request.getParameter("pageNum").isEmpty())
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		if(request.getParameter("cid")!=null && !request.getParameter("cid").isEmpty())
			cid = Integer.parseInt(request.getParameter("cid"));
		int rows = new MovieDaoImpl().getAll().size();
		int totalPage =(int)Math.ceil((double)rows/pageSize);
		List<Movie> movieList = new MovieDaoImpl().paging(cid, title, pageNum, pageSize);
		request.setAttribute("movies", movieList);
		request.setAttribute("totalPage", totalPage);
		request.setAttribute("categories", categoryList);
		request.setAttribute("pageNum", pageNum);
		System.out.println(totalPage);
		request.getRequestDispatcher("movieList.jsp").forward(request, response);
	}

}
